package com.zam.web.sysController;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zam.comment.Result;
import com.zam.pojo.Role;
import com.zam.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/******************************
 controller层
 ******************************/
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    /*
    添加数据
    */
    @PostMapping
    public Result save(@RequestBody Role role) {
        roleService.saveOrUpdate(role);
        return Result.success();
    }


    /*
    删除数据
    */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        roleService.removeById(id);
        return Result.success();
    }

    /*
    删除多个数据
    */
    @DeleteMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        roleService.removeByIds(ids);

        return Result.success();
    }

    /*
     查看全部
    */
    @GetMapping
    public Result findAll() {
        return Result.success(roleService.list());
    }


    /*
    根据id查看
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(roleService.getById(id));
    }

    /*
    分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer num, @RequestParam Integer pagesize, Role role) {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(role.getName())) {
            lambdaQueryWrapper.like(Role::getName, role.getName());
        }
        lambdaQueryWrapper.orderByDesc(Role::getId);
        Page<Role> rolePage = roleService.page(new Page<>(num, pagesize), lambdaQueryWrapper);
        return Result.success(rolePage);
    }
}

